package top.wshape1.rpc.config;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import top.wshape1.rpc.common.constant.RegistryType;

import java.util.List;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2024-02-04
 */

@Data
public class RegistryConfig {

    private String type = RegistryType.zookeeper.name();

    private RedisRegistryConfig redis;

    private ZookeeperRegistryConfig zookeeper = new ZookeeperRegistryConfig();

    private NacosRegistryConfig nacos;


    @Data
    @NoArgsConstructor
    public static class RedisRegistryConfig {
        private String host = "localhost";
        private int port = 6379;
        private String username;
        private String password;
        private int db = 0;
        /**
         * 在redis中存储的Key的前缀
         */
        private String prefix = "jjrpc";
        /**
         * 连接超时时间 默认2000ms
         */
        private int connectionTimeout = 2000;

        /**
         * 读取超时时间 默认3000ms
         */
        private int soTimeout = 3000;

        private boolean ssl = false;
        /**
         * redis线程池
         */
        private GenericObjectPoolConfig<Jedis> pool = new GenericObjectPoolConfig<>();

        public RedisRegistryConfig(String host, int port) {
            this.host = host;
            this.port = port;
        }

        public RedisRegistryConfig(String host, int port, String username, String password) {
            this.host = host;
            this.port = port;
            this.username = username;
            this.password = password;
        }
    }

    @Data
    @NoArgsConstructor
    public static class ZookeeperRegistryConfig {
        /**
         * connectString: 'ip:port' or 'ip1:port1,ip2:port2,...'
         */
        private String address = "localhost:2181";
        private String rootNode = "jjrpc";
        /**
         * ACL: List of 'scheme:id:perm'
         */
        private List<String> acl;
        /**
         * 会话超时 单位ms
         */
        private int sessionTimeout = 3000;
        /**
         * 连接超时 单位ms
         */
        private int connectionTimeout = 2000;

        public ZookeeperRegistryConfig(String address) {
            this.address = address;
        }
    }

    @Data
    @NoArgsConstructor
    public static class NacosRegistryConfig {
        private String address = "localhost:8848";
        private String group = "JJRPC";
        /**
         * 是否启用Nacos负载均衡，启用后服务发现只会返回一个服务实例，JJRPC提供的负载均衡和部分容错机制将失效
         */
        private boolean enableNacosLoadBalance = false;
    }

}
